package com.cloud.shopping.druid.controller;

import com.cloud.shopping.druid.model.HotGoods;
import com.cloud.shopping.druid.service.HotGoodsService;
import com.cloud.shopping.druid.util.DruidPage;
import com.cloud.shopping.tool.RespResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author John.zhang
 * @version Id: HotGoodsController, v 1.0 2021/2/8 9:33 ZhiYong Exp $
 */

@RestController
@RequestMapping(value = "/hot/goods")
public class HotGoodsController {

    @Resource
    private HotGoodsService hotGoodsService;

    /**
     * 热门商品分析
     * @param size size
     * @param hour hour
     * @param max max
     * @param urls urls
     * @return List
     */
    @PostMapping(value = "/search/{size}/{hour}/{max}")
    public RespResult<List<Map<String,String>>> searchHotGoods(@PathVariable("size")Integer size,
                                                               @PathVariable("hour")Integer hour,
                                                               @PathVariable("max")Integer max,
                                                               @RequestBody String[] urls){
        List<Map<String,String>> hotGoods = hotGoodsService.searchHotGoods(size, hour,urls,max);
        return RespResult.ok(hotGoods);
    }

    /**
     * 时间查询
     * @param size size
     * @param hour hour
     * @param urls urls
     * @return List
     */
    @PostMapping(value = "/search/{size}/{hour}")
    public RespResult<List<HotGoods>> search(@PathVariable("size")Integer size,
                                             @PathVariable("hour")Integer hour,
                                             @RequestBody String[] urls){
        List<HotGoods> hotGoods = hotGoodsService.search(size, hour,urls);
        return RespResult.ok(hotGoods);
    }

    /**
     * 时间查询
     * @param size size
     * @param hour hour
     * @return List
     */
    @GetMapping(value = "/search/{size}/{hour}")
    public RespResult<List<HotGoods>> search(@PathVariable("size")Integer size,
                                             @PathVariable("hour")Integer hour){
        List<HotGoods> hotGoods = hotGoodsService.search(size, hour);
        return RespResult.ok(hotGoods);
    }

    /**
     * 分页查询+排序
     * @param page page
     * @param size size
     * @param sort sort
     * @param sortType sortType
     * @return DruidPage
     */
    @GetMapping(value = "/page/{page}/{size}/{sort}/{sortType}")
    public RespResult<DruidPage<List<HotGoods>>> pageListSort(
            @PathVariable(value = "page")Integer page,
            @PathVariable(value = "size")Integer size,
            @PathVariable(value = "sort")String sort,
            @PathVariable(value = "sortType")String sortType){
        DruidPage<List<HotGoods>> druidPage = hotGoodsService.pageListSort(size, page,sort,sortType);
        return RespResult.ok(druidPage);
    }

    /**
     * 分页查询
     * @param page page
     * @param size size
     * @return DruidPage
     */
    @GetMapping(value = "/page/{page}/{size}")
    public RespResult<DruidPage<List<HotGoods>>> pageList(
            @PathVariable(value = "page")Integer page,
            @PathVariable(value = "size")Integer size){
        DruidPage<List<HotGoods>> druidPage = hotGoodsService.pageList(size, page);
        return RespResult.ok(druidPage);
    }

    /**
     * 查询前N条记录
     * @param size size
     * @return List
     */
    @GetMapping(value = "/top/{size}")
    public RespResult<List<HotGoods>> topNum(@PathVariable(value = "size")Integer size){
        List<HotGoods> hotGoods = hotGoodsService.topNum(size);
        return RespResult.ok(hotGoods);
    }

    /***
     * 查询所有
     * @return List
     */
    @GetMapping
    public RespResult<List<HotGoods>> list(){
        List<HotGoods> list = hotGoodsService.list();
        return RespResult.ok(list);
    }
}
